package com.loyal.mwm.ui

import android.text.TextUtils
import android.view.View
import com.loyal.mwm.R
import com.loyal.mwm.base.BaseSwipeActivity
import com.loyal.mwm.beans.ObservableAccountBean
import com.loyal.mwm.databinding.ActivityRegisterBinding
import com.loyal.mwm.handler.RegisterHandler
import com.loyal.mwm.impl.TextChangedListener
import com.loyal.mwm.util.ImageUtil

class RegisterActivity : BaseSwipeActivity<ActivityRegisterBinding>() {
    override fun actLayoutRes(): Int {
        return R.layout.activity_register
    }

    override fun afterOnCreate() {
        val toolbar = binding.toolbar.root
        setSupportActionBar(toolbar)
        binding.drawable = ImageUtil.getBackground(this)
        val observableAccountBean = ObservableAccountBean()
        binding.observableAccountBean = observableAccountBean
        val account = username
        observableAccountBean.username.set(account)
        observableAccountBean.editable.set(TextUtils.isEmpty(account))
        val fromLogin: Boolean = TextUtils.isEmpty(account)
        val extra = intent.getStringExtra("extra")
        binding.click = RegisterHandler(this, binding)
        initViews(fromLogin, extra)
    }

    private fun initViews(fromLogin: Boolean, extra: String?) {
        val toolbar = binding.toolbar.root
        if (TextUtils.isEmpty(extra)) {
            toolbar.title = if (fromLogin) "快速注册" else "修改密码"
            binding.accountClear.setImageResource(if (fromLogin) R.drawable.icon_clear else R.color.white)
            binding.accountClear.isEnabled = fromLogin
        } else {
            toolbar.setTitle(R.string.destroy_account)
            binding.accountClear.setImageResource(R.color.white)
            binding.accountClear.isEnabled = false
            binding.password.visibility = View.GONE
            binding.repeatMm.visibility = View.GONE
        }
        binding.account.addTextChangedListener(
            TextChangedListener(
                binding.accountClear
            )
        )
        binding.nickname.addTextChangedListener(
            TextChangedListener(
                binding.nicknameClear
            )
        )
        binding.password.addTextChangedListener(
            TextChangedListener(
                binding.passwordClear
            )
        )
        binding.repeatMm.addTextChangedListener(
            TextChangedListener(
                binding.repeatClear
            )
        )
    }

    override fun setEdgePosition(): Int {
        return LEFT
    } /*@OnClick({
            R.id.account_clear, R.id.nickname_clear,
            R.id.password_clear, R.id.repeat_clear
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.account_clear:
                binding.username.setText("");
                break;
            case R.id.nickname_clear:
                binding.nickname.setText("");
                break;
            case R.id.password_clear:
                binding.password.setText("");
                break;
            case R.id.repeat_clear:
                binding.repeatMm.setText("");
                break;
        }
    }*/
}